<template>
	<view class="margin">
		<view class="uni-text-small uni-text-gray margin-bottom">请使用PDA侧边扫码按键扫码,或者使用摄像头扫码</view>
		<uni-easyinput :focus="true" :inputBorder="true" :styles="styles" :placeholderStyle="placeholderStyle"
			class="uni-mt-5" suffixIcon="search" v-model="value" placeholder="SN" @iconClick="iconClick"
			@confirm="confirm">
		</uni-easyinput>
		
		<button :class="['cu-btn block line-orange lg margin-top-sm text-theme',cueTheme]"  @click="onCamera">
			<text class="cuIcon-camera"></text> 摄像头扫码</button>
		<view class="img-a">

		</view>
	</view>
</template>

<script>
	import * as api from 'common/api.js';
	export default {
		data() {
			return {
				value: '',
				password: '',
				placeholderStyle: "font-size:14px;borderColor: '#e78c1e'",
				styles: {
					//color: '#e78c1e',
					borderColor: '#e78c1e'
				}
			}
		},
		onLoad() {
			//监听Android按键事件
			// #ifdef APP-PLUS
			plus.key.addEventListener('keydown', event => {
				console.log("按键：" + event.keyCode);
				console.log(event);
			});
			// #endif
		},
		onReady() {},
		methods: {
			input(e) {
				console.log('输入内容：', e);
			},
			iconClick(type) {
				
				console.info("iconClick")
				this.confirm()
			},
			onCamera(){
				var that = this
				console.info("onCamera")
				// 只允许通过相机扫码
				uni.scanCode({
					onlyFromCamera: true,
					autoZoom:false,
					success: function (res) {
						console.log('条码类型：' + res.scanType);
						console.log('条码内容：' + res.result);
						that.value = res.result
						that.confirm()
					},
					complete(){
						console.log('complete');
					}
					
				});
			},
			confirm() {
				
				console.info(this.value)
				//判断格式是否正确
				if(this.value.length==0){
					uni.showModal({
						title: "提示",
						content: "请输入正确格式的SN码",
						showCancel: false,
						confirmText: "确定"
					})
					return
				}
				var sn_arr = this.value.split("#")
				if (sn_arr.length != 2) {
					uni.showModal({
						title: "提示",
						content: "错误的编码,请重试",
						showCancel: false,
						confirmText: "确定"
					})
					return
				} else {
					var bind_saddr = sn_arr[0]
					var bind_m_s = 0
					if (sn_arr[1] == "1") {
						bind_m_s = 0
					} else if (sn_arr[1] == "2") {
						bind_m_s = 1
					} else {
						uni.showModal({
							title: "提示",
							content: "错误的编码,请重试",
							showCancel: false,
							confirmText: "确定"
						})
						return
					}
					//请求完善的信息

					var that = this
					var pda_id = uni.getStorageSync('pda_id');
					uni.showLoading({
						title: '加载中'
					})
					uni.request({
						header: {
							'content-type': 'application/x-www-form-urlencoded'
						},
						url: api.API_HEAD + '/load_room',
						method: 'POST',
						data: {
							"pda_id": pda_id,
							"bind_saddr": bind_saddr,
							"bind_m_s": bind_m_s
						},
						success: res => {
							//console.info("success");
							console.info(JSON.stringify(res.data));
							if (res.data) {
								//console.info(res.data.data);
								if (res.data.code >= 0) {
									console.info(JSON.stringify(res.data.data));
									var item = res.data.data
									console.info(res.data.data.bind_addr)
									uni.redirectTo({
										url: "/pages/room/room?bind_addr=" + item.bind_addr +
											"&bind_saddr=" + item.bind_saddr + "&bind_m_s=" + item
											.bind_m_s + "&row=" + item.row + "&col=" + item.col
									})
								} else {
									uni.showToast({
										title: res.data.msg
									})
								}

							}
						},
						fail: (err) => {
							uni.showToast({
								title: '请求接口失败！'
							})

						},
						//请求结束之后，执行的回调函数（成功或失败都会执行）
						complete() {
							//隐藏loading
							uni.hideLoading()
						}
					});
				}


			}
		}
	}
</script>

<style>
	.img-a {
		width: 100%;
		height: 650rpx;
		background-image: url(../../static/img/scan_bg1.png);
		background-size: 100%;
		/* background-color: #e78c1e; */
	}
</style>